﻿//-----------------------------------------------------------------------------
// Copyright (c) BaFang Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace BaFangSpider
{
    using System;
    using System.Collections.Generic;
    using LumiSoft.Net.POP3.Client;
    using LumiSoft.Net.Mail;

    /// <summary>
    /// Summary description for ReceiveMail
    /// </summary>
    public static class ReceiveMail
    {
        public static IList<Mail_Message> Receive(string address)
        {
            Logger logger = Logger.GetLogger();
            logger.Write(DateTime.Now.ToString() + "start receive mail");
            POP3_Client pop3 = new POP3_Client();
            IList<Mail_Message> mails = new List<Mail_Message>();
            try
            {
                pop3.Connect("pop.gmail.com", 995, true);
                pop3.Authenticate(address, "guojia21@", true);
                foreach (POP3_ClientMessage message in pop3.Messages)
                {
                    Mail_Message mail = Mail_Message.ParseFromByte(message.MessageToByte());
                    mails.Add(mail);
                }
            }
            catch (Exception e)
            {
                logger.Write(DateTime.Now.ToString() + ":receive mail fail." + e.Message);
            }
            finally
            {
                pop3.Dispose();
            }
            return mails;
        }
    }
}